package org.oschina.api.software.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.software.model.Software;
import org.oschina.api.software.service.SoftwareService;
import org.oschina.api.software.vo.SoftwareAccessVo;
import org.oschina.api.software.vo.SoftwareVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.HtmlCleanFileter;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.oschina.handler.validator.filter.UploadFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/software")
public class SoftwareController extends BaseController {

	@Autowired
	private SoftwareService softwareService;
	
	/**
	 * 关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/create",method=RequestMethod.POST)
	public Result createFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.createFavorite(id);
	}
	
	/**
	 * 删除关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/delete",method=RequestMethod.POST)
	public Result deleteFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.deleteFavorite(id);
	}
	
	/**
	 * 根据搜索返回
	 * @param keywords
	 * @param first
	 * @param max
	 * @return
	 */
	@Validate
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public Pager<SoftwareVo> findVoBySearch
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String keyword,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return softwareService.findVoBySearch(keyword, pn);
	}
	
	/**
	 * 设置推荐
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/setRecommend/{id}",method=RequestMethod.POST)
	public Result setRecommend
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.setRecommend(id);
	}
	
	/**
	 * 编辑
	 * @param software
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public Result edit
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="home_page",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="doc_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="download_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="introduce",filter={HtmlCleanFileter.class})
		}) Software software
	)
	{
		return softwareService.edit(software);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public SoftwareAccessVo findById
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.findById(id);
	}
	
	/**
	 * 返回推荐
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/recommend",method=RequestMethod.GET)
	public Pager<SoftwareVo> findVoOrderRecommend(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return softwareService.findVoOrderRecommend(pn);
	}
	
	/**
	 * 返回首页推荐
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/recommend/index",method=RequestMethod.GET)
	public List<SoftwareVo> findVoOrderRecommendIndex()
	{
		return softwareService.findVoOrderRecommendIndex();
	}
	
	/**
	 * 返回正在审核的
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/check",method=RequestMethod.GET)
	public Pager<SoftwareVo> findCheck(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return softwareService.findCheck(pn);
	}
	
	/**
	 * 审核通过
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/check/pass/{id}",method=RequestMethod.POST)
	public Result checkPass
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.checkPass(id);
	}
	
	/**
	 * 审核失败
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/check/failure/{id}",method=RequestMethod.POST)
	public Result checkFailure
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return softwareService.checkFailure(id);
	}
	
	/**
	 * 创建
	 * @param software
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="home_page",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="doc_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="download_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="introduce",filter={HtmlCleanFileter.class})
		}) Software software
	)
	{
		return softwareService.create(software);
	}
	
	/**
	 * 更新
	 * @param software
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="title",filter={TextOnlyFilter.class}),
			@ValidateField(name="icon",filter={TextOnlyFilter.class,UploadFilter.class}),
			@ValidateField(name="home_page",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="doc_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="download_url",notNull=false,filter={TextOnlyFilter.class}),
			@ValidateField(name="introduce",filter={HtmlCleanFileter.class})
		}) Software software
	)
	{
		return softwareService.update(software);
	}
}